<?php
class AdminAction extends Action{
    
    protected $lang_file = 'common.php';
    
	public function index() 
    {
		$this->display();
	} 
    
    public function managegroup()
    {
        $model = D('LangGroup');
        import('ORG.Util.Page');
        $count = $model->count();
        $page = new Page($count,3);
        $pager = $page->show();
        $groups = $model->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('groups',$groups);
        $this->assign('pager',$pager);
        $this->display();
    }
    
    public function addgroup()
    {//添加分组
        if(isset($_POST) && count($_POST) > 0) 
        {
            $t_model = D('LangGroup');
            $data['key'] = trim(strtolower($_POST['key']));
            $data['title'] = trim($_POST['title']); 
            if($data['key'] == '' || $data['title'] == '')
            {
                $this->error('*请填写相关必要信息');
            }
            $condition['key'] = $data['key'];
            if(count($t_model->where($condition)->select()) > 0) 
            {
                $this->error('已存在相同的key分组~!');
            }
            $t_model->create($data);
            if($t_model->add())
            {
                $this->success('添加成功 ~！');die();
            } 
        }
        $this->display();
    }
    public function addmodule()
    { //添加模块 
        if(isset($_POST) && count($_POST) > 0) 
        {
            $t_model = D('LangModule');
            $data['group_id'] =  trim(strtolower($_POST['group']));
            $data['key'] = trim(strtolower($_POST['key']));
            $data['title'] = trim($_POST['title']); 
            if($data['key'] == '' || $data['title'] == '')
            {
                $this->error('*请核实填写相关必要信息');
            }
            $condition['group_id'] = $data['group_id'];
            $condition['key'] = $data['key'];
            if(count($t_model->where($condition)->select()) > 0) 
            {
                $this->error('此分组下已存在相同的key模块~!');
            } 
            $t_model->create($data);
            if($t_model->add())
            {
                $this->success('添加成功 ~！');die();
            } 
        }
        $model = D('LangGroup');
        $groups = $model->field('id,title')->select();
        if(count($groups) == 0)
        {
            $this->error('还没有发现分组，请先添加分组 ~！',U('addgroup'));
        }
        $this->assign('groups',$groups);
        $this->display();
    }
    
    public function addtranslate()
    {//添加翻译元素
        if(isset($_POST) && count($_POST) > 0) 
        {
            $t_model = D('LangTranslate');
            $data['group_id'] =  trim(strtolower($_POST['group']));
            $data['module_id'] =  trim(strtolower($_POST['module']));
            $data['key'] = trim(strtolower($_POST['key'])); 
            if($data['key'] == '' || $data['group_id'] == '' || $data['module_id'] == '')
            {
                $this->error('*请核实填写相关必要信息');
            } 
            $data['zh-cn'] = $_POST['zh-cn'];
            if($data['zh-cn'] == '') 
            {
                $this->error('简体中文为默认语言，必须填写~！');
            }
            $data['zh-tw'] = $_POST['zh-tw'];
            $data['en'] = $_POST['en'];
            $data['ru'] = $_POST['ru'];
            $data['fr'] = $_POST['fr'];
            $data['de'] = $_POST['de'];
            $data['ko'] = $_POST['ko'];
            $data['ja'] = $_POST['ja'];
            $data['ar'] = $_POST['ar'];
            $data['es'] = $_POST['es'];
            $data['pt'] = $_POST['pt'];
            $data['it'] = $_POST['it'];
            $data['nl'] = $_POST['nl'];
            $data['la'] = $_POST['la'];
            $condition['group_id'] = $data['group_id'];
            $condition['module_id'] = $data['module_id'];
            $condition['key'] = $data['key'];
            if(count($t_model->where($condition)->select()) > 0) 
            {
                $this->error('此分组模块下存在相同的key元素~!');
            } 
            $t_model->create($data);
            if($t_model->add())
            {
                $this->success('添加成功 ~！');die();
            } 
        }
        $model = D('LangGroup');
        $groups = $model->field('id,title')->select();
        if(count($groups) == 0)
        {
            $this->error('还没有发现分组，请先添加分组 ~！',U('addgroup'));
        }
        $this->assign('groups',$groups);
        $this->display();
    }
    public function reloadmodule()
    { //分组 -- 模块 联动
        if($this->isAjax()) {
            $condition['group_id'] = trim($_POST['group_id']); 
            $model = D('LangModule');
            $modules = $model->where($condition)->field('id,title')->select();
            if(count($modules) == 0)
            {
                echo '<option value="">此分组下无模块，请添加</option>'; 
                return;
            }
            foreach($modules as $module)
            {
                echo '<option value="' . $module['id'] . '">' . $module['title'] . '</option>';
            } 
        }
        else 
        {
            $this->error("What's going on ~!?",U('index'));
        }
    }
    
    public function relocale()
    {
        $this->relocalefile(); 
        //$this->display();
    }
    protected function relocalefile()
    {
        $g_model = D('LangGroup');
        $m_model = D('LangModule');
        $t_model = D('LangTranslate');
        $lang_list = C('LANG_LIST');
        $lang_arr = explode(',', $lang_list);
        $diy_arr = array(
            'zh-cn', //简体中文  1
            'zh-tw', //繁体中文  1
            'en', //英語   1
            'ru', //俄語  1
            'fr', //法語   1
            'de', //德语   1
            'ko', //韩语   1
            'ja', //日语   1
            'ar', //阿拉伯语  1
            'es', //西班牙语  1
            'pt', //葡萄牙語  1
            'it', //意大利語  1
            'nl', //荷兰語   1
            'la', //拉丁语   1
        );
        $success_msg = '更新语言文件: <br>';
        foreach($lang_arr as $lang)
        {
            if(in_array($lang,$diy_arr))
            { 
                $success_msg .= $lang .' 语言名称正常， ...';
                $elements = $t_model->field('id,group_id,module_id,key,final_key,'.$lang)->select();
                ob_start(); 
                echo  '<?php';echo "\r\n";
                echo  'return array(';echo "\r\n"; 
                foreach($elements as $element)
                {
                    $condition['id'] = $element['group_id'];
                    $group = $g_model->where($condition)->field('key')->find();
                    $condition['id'] = $element['module_id'];
                    $module = $m_model->where($condition)->field('key')->find();
                    $data['final_key'] = $group['key'] . '_' . $module['key'] . '_' . $element['key'];
                    $condition['id'] = $element['id'];
                    $t_model->where($condition)->save($data); //拼装多语言元素的key，并存入数据库
                    echo "\t";
                    echo "'".$data['final_key']."' => '" .$element[$lang]. "',";
                    echo "\r\n";
                } 
                echo  '); ?>';
                $t_locale = ob_get_contents();
                ob_end_clean();
                $t_lang_dir = BP . LANG_PATH . '/' .$lang; 
                if(!is_dir($t_lang_dir)) 
                {
                    mkdir($t_lang_dir);
                } 
                $t_lang_file = $t_lang_dir . '/' . $this->lang_file;
                if(file_put_contents($t_lang_file,$t_locale))
                {
                    $success_msg .= '... renew success~!<br>';
                }  
            }
            else
            {
                echo '<div style="background:#F2DEDE;padding:10px 20px;margin:20px;border:1px dotted #f99;display:block;">' . $lang . ' 语言名称异常，请核实 配置文件中 LANG_LIST 的值</div>';
            }
        } 
        $this->success($success_msg,U('index'),5); 
    }
    
    

	public function test() 
    {   echo "Just For Test<hr>";
    
        /*
        ob_start();
        echo htmlspecialchars('<?php');
        $t = ob_get_contents();
        ob_end_clean();
        file_put_contents(BP . '/Lang/zh-cn/test.php',$t);
        
        /*
		$backend_lang_model = D('BackendLang');
		$t = $backend_lang_model->find(2);
		print_r($t);
		echo '<hr>';
		$frontend_lang_model = D('FrontendLang');
		jeenShow($frontend_lang_model);
		jeenShow($frontend_lang_model->getDbFields());
		jeenShow($frontend_lang_model->getPk());
		$data['key'] = 'frontend_home_test';
		$data['zh-cn'] = '测试';
		$data['zh-tw'] = '測試';
		$data['en'] = 'Test';
		$data['ko'] = '테스트';
		$frontend_lang_model->create($data);
		if($frontend_lang_model->add())
			echo 'yes ! add success !';
		else
			echo 'something wrong when data add'; 
        
        /*
        ob_start(); 
        echo  '<?php';echo "\r\n";
        echo  'return array(';echo "\r\n";
        echo "\t";
        echo  '"test" => "测试",';
        echo "\r\n";
        echo  '); ?>';
        $t = ob_get_contents();
        ob_end_clean();
        file_put_contents(BP . '/Lang/zh-cn/test.php',$t);
        $t = file_get_contents(BP . '/Lang/zh-cn/test.php');
        jeenShow($t);*/
	}
      
    
//---Old functions---------------------------------------------------------- 
    /*
    public function renew()
    { //重新生成语言文件
        $f_model = D('FrontendLang');
        $b_model = D('BackendLang'); 
        $lang_list = C('LANG_LIST');
        $lang_arr = explode(',', $lang_list);
        $diy_arr = array(
            'zh-cn', //简体中文  1
            'zh-tw', //繁体中文  1
            'en', //英語   1
            'ru', //俄語  1
            'fr', //法語   1
            'de', //德语   1
            'ko', //韩语   1
            'ja', //日语   1
            'ar', //阿拉伯语  1
            'es', //西班牙语  1
            'pt', //葡萄牙語  1
            'it', //意大利語  1
            'nl', //荷兰語   1
            'la', //拉丁语   1
        );
        $success_msg = '更新语言文件: <br>';
        foreach($lang_arr as $lang)
        { 
            if(in_array($lang, $diy_arr))
            {
                $success_msg .= $lang .' 语言名称正常，正在更新...<br>';
                //读取Backend 表数据，并更新 至翻译文件
                $b_elements = $b_model->field('key,' . $lang)->select(); 
                $b_filename = 'backend.php';
                ob_start(); 
                echo  '<?php';echo "\r\n";
                echo  'return array(';echo "\r\n";
                foreach($b_elements as $b_element)
                {
                    echo "\t";
                    echo "'" .$b_element['key'] ."' => '".$b_element[$lang]."',";
                    echo "\r\n";
                }
                echo  '); ?>';
                $t_locale = ob_get_contents();
                ob_end_clean();
                $b_lang_file = BP . LANG_PATH . '/' .$lang . '/' . $b_filename;
                if(file_put_contents($b_lang_file,$t_locale))
                {
                    $success_msg .= 'Backend Lang renew success~!<br>';
                }
                //读取Frontend 表数据，并更新 至翻译文件
                $f_elements = $f_model->field('key,' . $lang)->select(); 
                $f_filename = 'frontend.php';
                ob_start();
                echo  '<?php';echo "\r\n";
                echo  'return array(';echo "\r\n";
                foreach($f_elements as $f_element)
                {
                    echo "\t";
                    echo "'" .$f_element['key'] ."' => '".$f_element[$lang]."',";
                    echo "\r\n";
                }
                echo  '); ?>';
                $t_locale = ob_get_contents();
                ob_end_clean();
                $f_lang_file = BP . LANG_PATH . '/' .$lang . '/' . $f_filename;
                if(file_put_contents($f_lang_file,$t_locale))
                {
                    $success_msg .= 'Frontend Lang renew success~!<br>';
                }
            }
            else
            {
                echo '<div style="background:#F2DEDE;padding:10px 20px;margin:20px;border:1px dotted #f99;display:block;">' . $lang . ' 语言名称异常，请核实 配置文件中 LANG_LIST 的值</div>';
            }
        }
        $this->success($success_msg,U('index'),15);
    }
    
	public function add() 
    { //添加翻译
        if(isset($_POST) && count($_POST) > 0) 
        { 
            if(isset($_POST['diy']) && $_POST['diy']=='yes') 
            {
                $key_arr = explode('_',$_POST['key']);
                if(count($key_arr) >= 3) 
                {
                    $group = strtolower($key_arr[0]);
                    $module = strtolower($key_arr[1]);
                    $key = strtolower(join('_',array_slice($key_arr,2)));
                }
                else
                {
                    $this->error('自定义key 不符合规范');
                }
            }
            else 
            {
                $group = strtolower($_POST['group']);
                $module = strtolower($_POST['module']);
                $key = strtolower($_POST['key']);
            }
            $data['key'] = $group . '_' . $module . '_' . $key; 
            $data['zh-cn'] = $_POST['zh-cn'];
            $data['zh-tw'] = $_POST['zh-tw'];
            $data['en'] = $_POST['en'];
            $data['ru'] = $_POST['ru'];
            $data['fr'] = $_POST['fr'];
            $data['de'] = $_POST['de'];
            $data['ko'] = $_POST['ko'];
            $data['ja'] = $_POST['ja'];
            $data['ar'] = $_POST['ar'];
            $data['es'] = $_POST['es'];
            $data['pt'] = $_POST['pt'];
            $data['it'] = $_POST['it'];
            $data['nl'] = $_POST['nl'];
            $data['la'] = $_POST['la'];
            if ($group == 'frontend')
            {
                $t_model = D('FrontendLang');
            }
            elseif($group == 'backend')
            {
                $t_model = D('BackendLang'); 
            }
            else 
            {
                $this->error('分组异常~！请检查key值是否符合规范~！<br>分组值 请参考 Group 中的选项~~');
            }
            $t_model->create($data);
            if($t_model->add())
            {
                $this->success('添加成功 ~！');
            }
            else 
            {
                $this->error('添加过程异常，(key 已存在)');
            }
            jeenShow($_POST); 
        }
		$this->display();
	}
     */
//---Old functions End------------------------------------------------------
    
}